﻿Imports System.Data.OleDb
Public Class ProfilesEdit
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'adding extra information about new users to UserProfiles table
        If Not IsPostBack Then
            Dim conn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("FinalConnectionString").ConnectionString)
            Dim sql = "SELECT * FROM UserProfiles WHERE ResearcherName=@f1"
            Dim cmdDB = New OleDbCommand(sql, conn)
            cmdDB.CommandType = CommandType.Text
            'We used RegisterUser.UserName instead of User.Identity.Name, because the new user name is not exist yet
            cmdDB.Parameters.AddWithValue("@f1", User.Identity.Name)
            conn.Open()
            'read data from UserProfile table
            Dim dr As OleDbDataReader = cmdDB.ExecuteReader()
            'This will check if there is any record in the table
            If dr.HasRows Then
                dr.Read()
                'IsDBNull is used whether there is null value in the fields or not
                If Not IsDBNull(dr("FirstName")) Then tb_userFirstName.Text = dr("FirstName")
                If Not IsDBNull(dr("LastName")) Then tb_userLastName.Text = dr("LastName")
                If Not IsDBNull(dr("Description")) Then tb_userDescription.Text = dr("Description")
                If Not IsDBNull(dr("Degree")) Then tb_degree.Text = dr("Degree")
                If Not IsDBNull(dr("University")) Then tb_university.Text = dr("University")
                If Not IsDBNull(dr("Country")) Then tb_country.Text = dr("Country")
                If Not IsDBNull(dr("Languages")) Then tb_languages.Text = dr("Languages")
                If Not IsDBNull(dr("Website")) Then tb_website.Text = dr("Website")
                If Not IsDBNull(dr("Email")) Then tb_email.Text = dr("Email")
                If Not IsDBNull(dr("FacebookAccount")) Then tb_facebookAccount.Text = dr("FacebookAccount")
                If Not IsDBNull(dr("TwitterAccount")) Then tb_twitterAccount.Text = dr("TwitterAccount")
                If Not IsDBNull(dr("LinkedInAccount")) Then tb_linkedInAccount.Text = dr("LinkedInAccount")
                If Not IsDBNull(dr("MobileNo")) Then tb_mobileNo.Text = dr("MobileNo")
                If Not IsDBNull(dr("TelephoneNo")) Then tb_telephoneNo.Text = dr("TelephoneNo")
            End If
            conn.Close()
        End If
    End Sub

    Protected Sub btn_userProfileSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_userProfileSubmit.Click
        Dim newUserProfilePictureName As String = ""
        Dim sqlString As String

        If fu_userProfilePicture.HasFile Then
            Dim myGUID = Guid.NewGuid()
            'Guid if to generate a random and unique user profile pictures
            newUserProfilePictureName = myGUID.ToString() & ".jpg"
            Dim userProfilePictureLocation = Request.MapPath("Profile_Pic") & "/" & newUserProfilePictureName
            fu_userProfilePicture.SaveAs(userProfilePictureLocation)
        End If

        Dim oleDbconn As New OleDbConnection(ConfigurationManager.ConnectionStrings("FinalConnectionString").ConnectionString)

        If fu_userProfilePicture.HasFile Then
            sqlString = "UPDATE UserProfiles SET FirstName=@f1,LastName=@f2,Description=@f3,Picture=@f4,Degree=@f6,University=@f7,Country=@f8,Languages=@f9,Website=@f10,Email=@f11,FacebookAccount=@f12,TwitterAccount=@f13,LinkedInAccount=@f14,MobileNo=@f15,TelephoneNo=@f16 WHERE ResearcherName=@f5"
        Else
            sqlString = "UPDATE UserProfiles SET FirstName=@f1,LastName=@f2,Description=@f3,Degree=@f6,University=@f7,Country=@f8,Languages=@f9,Website=@f10,Email=@f11,FacebookAccount=@f12,TwitterAccount=@f13,LinkedInAccount=@f14,MobileNo=@f15,TelephoneNo=@f16 WHERE ResearcherName=@f5"
        End If
        Dim cmdDB = New OleDbCommand(sqlString, oleDbconn)
        cmdDB.CommandType = CommandType.Text
        cmdDB.Parameters.AddWithValue("@f1", tb_userFirstName.Text)
        cmdDB.Parameters.AddWithValue("@f2", tb_userLastName.Text)
        cmdDB.Parameters.AddWithValue("@f3", tb_userDescription.Text)
        If fu_userProfilePicture.HasFile Then cmdDB.Parameters.AddWithValue("@f4", "Profile_Pic/" & newUserProfilePictureName)
        cmdDB.Parameters.AddWithValue("@f6", tb_degree.Text)
        cmdDB.Parameters.AddWithValue("@f7", tb_university.Text)
        cmdDB.Parameters.AddWithValue("@f8", tb_country.Text)
        cmdDB.Parameters.AddWithValue("@f9", tb_languages.Text)
        cmdDB.Parameters.AddWithValue("@f10", tb_website.Text)
        cmdDB.Parameters.AddWithValue("@f11", tb_email.Text)
        cmdDB.Parameters.AddWithValue("@f12", tb_facebookAccount.Text)
        cmdDB.Parameters.AddWithValue("@f13", tb_twitterAccount.Text)
        cmdDB.Parameters.AddWithValue("@f14", tb_linkedInAccount.Text)
        cmdDB.Parameters.AddWithValue("@f15", tb_mobileNo.Text)
        cmdDB.Parameters.AddWithValue("@f16", tb_telephoneNo.Text)
        cmdDB.Parameters.AddWithValue("@f5", User.Identity.Name)
        oleDbconn.Open()
        cmdDB.ExecuteNonQuery()
        'Dispose will erase every objects in memory to be cleared before going to another page
        cmdDB.Dispose()
        oleDbconn.Close()
        oleDbconn.Dispose()
        Response.Redirect("Profiles.aspx?ResearcherName=" & User.Identity.Name)
    End Sub
End Class